package joke.servlet;

import java.io.IOException;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import joke.deal.ReComic;

public class GetComicMagicServlet extends HttpServlet {
	private static final String CONTENT_TYPE = "image/jpeg";

	private static final long serialVersionUID = 1L;

	public void init(ServletConfig config) throws ServletException {
		super.init(config);
	}

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setContentType(CONTENT_TYPE);
		String id;
		ServletOutputStream out = response.getOutputStream();
		try {
			id = request.getParameter("id");
		} catch (Exception e) {
			e.printStackTrace();
			response.sendRedirect("http://www.ecook.cn");
			return;
		}
		try {
			ReComic rc = new ReComic();
			byte buf[] = rc.getImage(id);
			out.write(buf);
		} catch (IOException ioe) {
			ioe.printStackTrace();
		}finally {
			try {
				if (out != null)
					out.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}
}
